package com.dropdb.demo.controller.admin;

import com.dropdb.demo.bean.Coupon;
import com.dropdb.demo.bean.vo.BaseData;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.service.PromotionService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qugr
 * @create 2020-06-26 16:53
 */
@RestController
@RequestMapping("admin/coupon")
public class AdminPromotionCouponController {

    @Autowired
    PromotionService promotionService;

    @RequestMapping("list")
    @RequiresPermissions(value = {"*", "admin:coupon:list"}, logical = Logical.OR)
    public BaseRespVO queryCoupons(Integer page, Integer limit, String name, Short type, Short status, String sort, String order){
        BaseData baseData = promotionService.queryCoupons(page, limit, name, type, status, sort, order);
        return BaseRespVO.ok(baseData);
    }

    @RequestMapping("create")
    @RequiresPermissions(value = {"*", "admin:coupon:create"}, logical = Logical.OR)
    public BaseRespVO createCoupon(@RequestBody Coupon coupon){
        int insert = promotionService.insertCoupon(coupon);
        if (insert != 0) {
            Coupon couponVO = promotionService.queryCoupon(coupon.getId());
            return BaseRespVO.ok(couponVO);
        }
        return BaseRespVO.error(500,"添加失败");
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"*", "admin:coupon:delete"}, logical = Logical.OR)
    public BaseRespVO deleteCoupon(@RequestBody Coupon coupon){
        int delete = promotionService.deleteCoupon(coupon.getId());
        if (delete != 1) {
            return BaseRespVO.error(500,"删除失败");
        }
        return BaseRespVO.ok();
    }

    @RequestMapping("read")
    @RequiresPermissions(value = {"*", "admin:coupon:read"}, logical = Logical.OR)
    public BaseRespVO readCoupon(Integer id){
        Coupon coupon = promotionService.queryCoupon(id);
        if (coupon != null) {
            return BaseRespVO.ok(coupon);
        }
        return BaseRespVO.error(500,"读取失败");
    }

    @RequestMapping("listuser")
    @RequiresPermissions(value = {"*", "admin:coupon:listuser"}, logical = Logical.OR)
    public BaseRespVO queryCouponUsers(Integer page, Integer limit, Integer couponId, Integer userId, Short status, String sort, String order){
        BaseData baseData = promotionService.queryCouponUsers(page, limit, couponId, userId, status, sort, order);
        return BaseRespVO.ok(baseData);
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"*", "admin:coupon:update"}, logical = Logical.OR)
    public BaseRespVO updateCoupon(@RequestBody Coupon coupon){
        int update = promotionService.updateCoupon(coupon);
        if (update != 1) {
            return BaseRespVO.error(500,"更新失败");
        }
        Coupon queryCoupon = promotionService.queryCoupon(coupon.getId());
        return BaseRespVO.ok(queryCoupon);
    }

}
